package com.swim.session;

import javax.ejb.Stateless;
import javax.persistence.Query;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.jboss.ejb3.annotation.LocalBinding;
import org.jboss.ejb3.annotation.RemoteBinding;

@Stateless
@RemoteBinding(jndiBinding="CleanDatabaseBeanJNDI")
@LocalBinding(jndiBinding="CleanDatabaseBeanLocalJNDI")
public class CleanDatabaseBean implements CleanDatabaseLocal,
		CleanDatabaseRemote {
	
	@PersistenceContext(unitName = "SWIM")
	private EntityManager database;
	
	public void clean() {
		Query q = database.createNativeQuery("set foreign_key_checks = 0");
		q.executeUpdate();
		

		q = database.createNativeQuery("delete f from Feedback f");
		q.executeUpdate();
		
		q = database.createNativeQuery("delete c from Collaboration c");
		q.executeUpdate();
		
		q = database.createNativeQuery("delete c from CollaborationRequest c");
		q.executeUpdate();
		
		q = database.createNativeQuery("delete f from FriendOf f");
		q.executeUpdate();
		
		q = database.createNativeQuery("delete f from FriendshipRequest f");
		q.executeUpdate();
		
		q = database.createNativeQuery("delete h from HasSkill h");
		q.executeUpdate();

		q = database.createNativeQuery("delete s from SkillRequest s");
		q.executeUpdate();
		
		q = database.createNativeQuery("delete n from Notifications n");
		q.executeUpdate();
		
		q = database.createNativeQuery("delete r from RegisteredUser r WHERE r.admin = 0");
		q.executeUpdate();
		
		q = database.createNativeQuery("delete s from Skill s");
		q.executeUpdate();
		
		q = database.createNativeQuery("set foreign_key_checks = 1");
		q.executeUpdate();
	}
}
